﻿using Md.Api.Entity.DapperModels.MdDB;
using Md.Api.Model.Manage.MdUser.Request;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 用户仓储
/// </summary>
public class MdUserRep : BaseRep<MdUserModel>
{
    private static string sqlFields = string.Format(@"
        `user_id`,
        `avatar`,
        `user_name`,
        `user_pwd`,
        `user_nick`,
        `user_type`,
        `dept_id`,
        `role_id`,
        `post_id`,
        `sex`,
        `email`,
        `phone`,
        `integral`,
        `on_line`,
        `status`,
        `del_flag`,
        `remark`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`,
        `login_ip`,
        `login_date`,
        `open_id`
    ");

    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <param name="userPwd">用户密码</param>
    /// <returns>MdUserModel</returns>
    public MdUserModel? Login(string? userName, string? userPwd)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `user_name` = @UserName and `user_pwd` = @UserPwd ");

        var res = QuerySingle(sqlFields, sqlWhere, new { UserName = userName, UserPwd = userPwd });

        return res;
    }

    /// <summary>
    /// 邮箱登录
    /// </summary>
    /// <param name="email">邮箱</param>
    /// <param name="userPwd">用户密码</param>
    /// <returns>MdUserModel</returns>
    public MdUserModel? LoginEmail(string? email, string? userPwd)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `email` = @Email and `user_pwd` = @UserPwd ");

        var res = QuerySingle(sqlFields, sqlWhere, new { Email = email, UserPwd = userPwd });

        return res;
    }

    /// <summary>
    /// 手机号登录
    /// </summary>
    /// <param name="phone">手机号</param>
    /// <param name="userPwd">用户密码</param>
    /// <returns>MdUserModel</returns>
    public MdUserModel? LoginPhone(string? phone, string? userPwd)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `phone` = @Phone and `user_pwd` = @UserPwd ");

        var res = QuerySingle(sqlFields, sqlWhere, new { Phone = phone, UserPwd = userPwd });

        return res;
    }

    /// <summary>
    /// 通过用户编号获取用户信息
    /// </summary>
    /// <param name="userId">用户编号</param>
    /// <returns>MdUserModel</returns>
    public MdUserModel? GetMdUserByUserId(long userId)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `user_id` = @UserId ");

        return QuerySingle(sqlFields, sqlWhere, new { UserId = userId });
    }

    /// <summary>
    /// 通过用户编号获取用户密码
    /// </summary>
    /// <param name="userId">用户编号</param>
    /// <returns>MdUserModel</returns>
    public MdUserModel? GetMdUserPwdByUserId(long userId)
    {
        string sqlFields = string.Format(@" `user_pwd`, `del_flag`, `status` ");

        string sqlWhere = string.Format(@" and `del_flag` = '0' and `user_id` = @UserId ");

        return QuerySingle(sqlFields, sqlWhere, userId);
    }

    /// <summary>
    /// 获取用户列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdUserModel></returns>
    public IEnumerable<MdUserModel> GetMdUserList(MdUserModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.DeptId > 0)
        {
            sqlWhere += " and `dept_id` = @DeptId ";
        }
        if (model.RoleId > 0)
        {
            sqlWhere += " and `role_id` = @RoleId ";
        }
        if (model.PostId > 0)
        {
            sqlWhere += " and `post_id` = @PostId ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserName))
        {
            sqlWhere += " and `user_name` like concat('%', @UserName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserNick))
        {
            sqlWhere += " and `nick_name` like concat('%', @UserNick, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserType))
        {
            sqlWhere += " and `user_type` in (@UserType) ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` like concat('%', @Email, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and `phone` like concat('%', @Phone, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.OnLine))
        {
            sqlWhere += " and `on_line` = @OnLine ";
        }
        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlWhere += " and `sex` = @Sex ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.OpenId))
        {
            sqlWhere += " and `open_id` = @OpenId ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取用户列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdUserModel></returns>
    public Page<MdUserModel> GetMdUserPageList(MdUserModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.DeptId > 0)
        {
            sqlWhere += " and `dept_id` = @DeptId ";
        }
        if (model.RoleId > 0)
        {
            sqlWhere += " and `role_id` = @RoleId ";
        }
        if (model.PostId > 0)
        {
            sqlWhere += " and `post_id` = @PostId ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserName))
        {
            sqlWhere += " and `user_name` like concat('%', @UserName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserNick))
        {
            sqlWhere += " and `nick_name` like concat('%', @UserNick, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserType))
        {
            sqlWhere += " and `user_type` in (@UserType) ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` like concat('%', @Email, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and `phone` like concat('%', @Phone, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.OnLine))
        {
            sqlWhere += " and `on_line` = @OnLine ";
        }
        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlWhere += " and `sex` = @Sex ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.OpenId))
        {
            sqlWhere += " and `open_id` = @OpenId ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过用户编号更新用户
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdUserByUserId(MdUserModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.Avatar))
        {
            sqlSet += " `avatar` = @Avatar, ";
        }

        if (model.DeptId > 0)
        {
            sqlSet += " `dept_id` = @DeptId, ";
        }

        if (model.RoleId > 0)
        {
            sqlSet += " `role_id` = @RoleId, ";
        }

        if (model.PostId > 0)
        {
            sqlSet += " `post_id` = @PostId, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UserName))
        {
            sqlSet += " `user_name` = @UserName, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UserNick))
        {
            sqlSet += " `user_nick` = @UserNick, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UserType))
        {
            sqlSet += " `user_type` = @UserType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlSet += " `email` = @Email, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlSet += " `phone` = @Phone, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlSet += " `sex` = @Sex, ";
        }

        if (model.Integral > 0)
        {
            sqlSet += " `integral` = @Integral, ";
        }

        if(!string.IsNullOrWhiteSpace(model.OnLine))
        {
            sqlSet += " `on_line` = @OnLine, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `user_Id` = @UserId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过用户编号更新用户密码
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdUserPwdByUserId(long userId, string userPwd)
    {
        string sqlSet = " `user_pwd` = @UserPwd ";

        string sqlWhere = " and `del_flag` = '0' and `user_Id` = @UserId ";

        return Update(sqlSet, sqlWhere, new { UserId = userId, UserPwd = userPwd });
    }

    /// <summary>
    /// 通过用户编号删除用户
    /// </summary>
    /// <param name="userIds">用户编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdUserByUserId(string userIds)
    {
        string sqlWhere = " and `user_id` in @UserIds ";

        return Delete(sqlWhere, new { UserIds = userIds.Split(',').ToArray() });
    }

    /// <summary>
    /// 检查用户名是否唯一
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <returns>object?</returns>
    public object? CheckUserNameUnique(string userName)
    {
        string sqlField = " `user_id` ";

        string sqlWhere = " and `del_flag` = '0' and `user_name` = @UserName ";

        return Scalar(sqlField, sqlWhere, new { UserName = userName }); ;
    }

    /// <summary>
    /// 检查用户手机号是否唯一
    /// </summary>
    /// <param name="phone">手机号</param>
    /// <returns>object?</returns>
    public object? CheckPhoneUnique(string phone)
    {
        string sqlField = " `user_id` ";

        string sqlWhere = " and `del_flag` = '0' and `phone` = @Phone ";

        return Scalar(sqlField, sqlWhere, new { Phone = phone });
    }

    /// <summary>
    /// 检查用户邮箱是否唯一
    /// </summary>
    /// <param name="email">邮箱</param>
    /// <returns>object?</returns>
    public object? CheckEmailUnique(string email)
    {
        string sqlField = " `user_id` ";

        string sqlWhere = " and `del_flag` = '0' and `email` = @Email ";

        return Scalar(sqlField, sqlWhere, new { Email = email });
    }
}